/*     */ package com.zimbra.qa.unittest.prov.soap;
/*     */ 
/*     */ import com.google.common.collect.Maps;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.CalendarResource;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.qa.unittest.prov.ProvTest.Sequencer;
/*     */ import com.zimbra.soap.admin.message.SetPasswordRequest;
/*     */ import com.zimbra.soap.admin.message.SetPasswordResponse;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector.GranteeBy;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.Map;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSetPassword
/*     */   extends SoapTest
/*     */ {
/*     */   private static SoapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static Domain domain;
/*     */   private static final int MIN_PASSSWORD_LEN = 5;
/*  50 */   private static final ProvTest.Sequencer goodPassword = new ProvTest.Sequencer();
/*  51 */   private static final ProvTest.Sequencer badPassword = new ProvTest.Sequencer();
/*     */   
/*     */   @BeforeClass
/*     */   public static void init() throws Exception {
/*  55 */     provUtil = new SoapProvTestUtil();
/*  56 */     prov = provUtil.getProv();
/*  57 */     domain = provUtil.createDomain(baseDomainName());
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  62 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*     */   }
/*     */   
/*     */   private static void setPasswordPolicy(Account acct) throws Exception {
/*  66 */     Map<String, Object> attrs = Maps.newHashMap();
/*  67 */     attrs.put("zimbraPasswordMinLength", "5");
/*  68 */     prov.modifyAttrs(acct, attrs);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private String genGoodPassword()
/*     */   {
/*  75 */     String password = "good-" + goodPassword.next();
/*  76 */     Assert.assertTrue(password.length() >= 5);
/*  77 */     return password;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private String genBadPassword()
/*     */   {
/*  84 */     String password = "b-" + badPassword.next();
/*  85 */     Assert.assertTrue(password.length() < 5);
/*  86 */     return password;
/*     */   }
/*     */   
/*     */   private String setPassword(Account authedAcct, Account targetAcct, boolean againstPolicy) throws Exception
/*     */   {
/*  91 */     String newPassword = againstPolicy ? genBadPassword() : genGoodPassword();
/*     */     
/*  93 */     SoapTransport transport = authAdmin(authedAcct.getName());
/*  94 */     SetPasswordRequest req = new SetPasswordRequest(targetAcct.getId(), newPassword);
/*  95 */     SetPasswordResponse resp = (SetPasswordResponse)invokeJaxb(transport, req);
/*     */     
/*  97 */     return newPassword;
/*     */   }
/*     */   
/*     */   private Account createAcctAndSetPasswordpolicy() throws Exception {
/* 101 */     Account acct = provUtil.createAccount(genAcctNameLocalPart("user"), domain);
/* 102 */     setPasswordPolicy(acct);
/* 103 */     return acct;
/*     */   }
/*     */   
/*     */   private CalendarResource createCRAndSetPasswordpolicy() throws Exception {
/* 107 */     CalendarResource cr = provUtil.createCalendarResource(genCalendarResourceNameLocalPart(), domain);
/* 108 */     setPasswordPolicy(cr);
/* 109 */     return cr;
/*     */   }
/*     */   
/*     */   private void verifyOK(Account adminAcct, Account userAccount, boolean againstPolicy) throws Exception
/*     */   {
/* 114 */     String newPassword = setPassword(adminAcct, userAccount, againstPolicy);
/* 115 */     authUser(userAccount.getName(), newPassword);
/*     */   }
/*     */   
/*     */   private void verifyPermDenied(Account adminAcct, Account userAccount, boolean againstPolicy) throws Exception
/*     */   {
/* 120 */     String errorCode = null;
/*     */     try {
/* 122 */       setPassword(adminAcct, userAccount, againstPolicy);
/*     */     } catch (ServiceException e) {
/* 124 */       errorCode = e.getCode();
/*     */     }
/* 126 */     Assert.assertEquals("service.PERM_DENIED", errorCode);
/*     */   }
/*     */   
/*     */   private void verifyInvalidPassword(Account adminAcct, Account userAccount, boolean againstPolicy) throws Exception
/*     */   {
/* 131 */     String errorCode = null;
/*     */     try {
/* 133 */       setPassword(adminAcct, userAccount, againstPolicy);
/*     */     } catch (ServiceException e) {
/* 135 */       errorCode = e.getCode();
/*     */     }
/* 137 */     Assert.assertEquals("account.INVALID_PASSWORD", errorCode);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void globalAdmin() throws Exception {
/* 142 */     Account acct = createAcctAndSetPasswordpolicy();
/* 143 */     CalendarResource cr = createCRAndSetPasswordpolicy();
/* 144 */     Account adminAcct = provUtil.createGlobalAdmin(genAcctNameLocalPart("admin"), domain);
/*     */     
/* 146 */     verifyOK(adminAcct, acct, true);
/* 147 */     verifyOK(adminAcct, acct, false);
/* 148 */     verifyOK(adminAcct, cr, true);
/* 149 */     verifyOK(adminAcct, cr, false);
/*     */     
/* 151 */     provUtil.deleteAccount(acct);
/* 152 */     provUtil.deleteAccount(cr);
/* 153 */     provUtil.deleteAccount(adminAcct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void delegatedAdminWithNoRight() throws Exception {
/* 158 */     Account acct = createAcctAndSetPasswordpolicy();
/* 159 */     CalendarResource cr = createCRAndSetPasswordpolicy();
/* 160 */     Account adminAcct = provUtil.createDelegatedAdmin(genAcctNameLocalPart("admin"), domain);
/*     */     
/* 162 */     verifyPermDenied(adminAcct, acct, true);
/* 163 */     verifyPermDenied(adminAcct, acct, false);
/* 164 */     verifyPermDenied(adminAcct, cr, true);
/* 165 */     verifyPermDenied(adminAcct, cr, false);
/*     */     
/* 167 */     provUtil.deleteAccount(acct);
/* 168 */     provUtil.deleteAccount(cr);
/* 169 */     provUtil.deleteAccount(adminAcct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void delegatedAdminWithSetPasswordRight() throws Exception {
/* 174 */     Account acct = createAcctAndSetPasswordpolicy();
/* 175 */     CalendarResource cr = createCRAndSetPasswordpolicy();
/* 176 */     Account adminAcct = provUtil.createDelegatedAdmin(genAcctNameLocalPart("admin"), domain);
/*     */     
/* 178 */     prov.grantRight(TargetType.account.getCode(), TargetBy.name, acct.getName(), GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.name, adminAcct.getName(), null, "setAccountPassword", null);
/*     */     
/*     */ 
/* 181 */     prov.grantRight(TargetType.calresource.getCode(), TargetBy.name, cr.getName(), GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.name, adminAcct.getName(), null, "setCalendarResourcePassword", null);
/*     */     
/*     */ 
/*     */ 
/* 185 */     verifyOK(adminAcct, acct, true);
/* 186 */     verifyOK(adminAcct, acct, false);
/* 187 */     verifyOK(adminAcct, cr, true);
/* 188 */     verifyOK(adminAcct, cr, false);
/*     */     
/* 190 */     provUtil.deleteAccount(acct);
/* 191 */     provUtil.deleteAccount(cr);
/* 192 */     provUtil.deleteAccount(adminAcct);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void delegatedAdminWithChangePasswordRight() throws Exception {
/* 197 */     Account acct = createAcctAndSetPasswordpolicy();
/* 198 */     CalendarResource cr = createCRAndSetPasswordpolicy();
/* 199 */     Account adminAcct = provUtil.createDelegatedAdmin(genAcctNameLocalPart("admin"), domain);
/*     */     
/* 201 */     prov.grantRight(TargetType.account.getCode(), TargetBy.name, acct.getName(), GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.name, adminAcct.getName(), null, "changeAccountPassword", null);
/*     */     
/*     */ 
/* 204 */     prov.grantRight(TargetType.calresource.getCode(), TargetBy.name, cr.getName(), GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.name, adminAcct.getName(), null, "changeCalendarResourcePassword", null);
/*     */     
/*     */ 
/*     */ 
/* 208 */     verifyInvalidPassword(adminAcct, acct, true);
/* 209 */     verifyOK(adminAcct, acct, false);
/* 210 */     verifyInvalidPassword(adminAcct, cr, true);
/* 211 */     verifyOK(adminAcct, cr, false);
/*     */     
/* 213 */     provUtil.deleteAccount(acct);
/* 214 */     provUtil.deleteAccount(cr);
/* 215 */     provUtil.deleteAccount(adminAcct);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/soap/TestSetPassword.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */